﻿
using Dt261.db;
using Dt261.jsonmodel;
using GalaSoft.MvvmLight;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dt261.ViewModel
{
    class PreviewSpeechViewModel: PreviewPageViewModel
    {

        SpeechResultDb resultdb;
        SpeechResultEntryDb entrydb;


        private SpeechResultItem speechResult;
        private MemberInfo selectMember;

        private ObservableCollection<ResultDataListItem> speechResultDatas;
        //private SpeechResultItem result;
        //private Model.MemberInfo member;

        public PreviewSpeechViewModel(SpeechResultItem speechResult, Model.MemberInfo selectMember)
        {

            resultdb = new SpeechResultDb(dbconfig.DatabaseFilename);
            entrydb = new SpeechResultEntryDb(dbconfig.DatabaseFilename);

            this.SpeechResult = speechResult;
            this.SelectMember = selectMember;

            if(speechResult != null )
            {
                if (speechResult.li != null)
                {


                    this.SpeechResultDatas = new ObservableCollection<ResultDataListItem>(speechResult.li);
                }
                else
                {
                    IList < ResultDataListItem > li = entrydb.GetSpeechItemsByResultId(speechResult.id);
                    this.SpeechResultDatas = new ObservableCollection<ResultDataListItem>(li);
                    //{
                    //    var http = Http.SharedHttp();
                    //    //http.Request.Accept = HttpContentTypes.ApplicationJson;
                    //    //http.Request.ContentType = HttpContentTypes.ApplicationXWwwFormUrlEncoded;







                    //    //user / getAudioResultPage   POST（非JSON）   查询听力信息 （分页）  personId = "4d573cb2bb9b4dca9db9c9db40021e4a"
                    //    //user / getAudioDataDetails ？ resultId = 4028fb6c8215bad7018215bbb5b20000 GET
                    //    string ret = http.GetResponse(null, Http.HOST + "tlyht/user/getAudioDataDetails?resultId=" +SpeechResult.id, "get");

                    //    //var response = http.Post(, dict, HttpContentTypes.ApplicationXWwwFormUrlEncoded);
                    //    Console.WriteLine(ret);

                    //    if (ret != null)
                    //    {


                    //        JObject o = (JObject)JsonConvert.DeserializeObject(ret);

                    //        if (null == Http.errorWithJsonObject(o))
                    //        {
                    //            SpeechResultItem info = JsonConvert.DeserializeObject<SpeechResultItem>(o["Data"].ToString());

                    //            this.SpeechResultDatas = new ObservableCollection<ResultDataListItem>(info.li);
                    //            // Notice.Show("言语测试结果已保存.", "提示", 3, MessageBoxIcon.Success);
                    //            //DMSkin.Core.Broadcast.PushBroadcast("UpdateHistory", "TestWindowViewModel");

                    //            //DMSkin.Core.Broadcast.PushBroadcast("CLOSEWINDOW", "SpeechWindowViewModel"); //("CLOSEWINDOW", (obj) =>



                    //            return;
                    //        }
                    //        else
                    //        {
                    //            Notice.Show("保存失败，请重试!.", "提示", 3, MessageBoxIcon.Error);
                    //            // this.Report = Http.errorWithJsonObject(o) + ",请重试!";
                    //        }

                    //    }



                    //}
                }








            }


        }

        public ObservableCollection<ResultDataListItem> SpeechResultDatas { get => speechResultDatas; set { Set(ref speechResultDatas, value); } }


        public SpeechResultItem SpeechResult { get => speechResult; set { speechResult = value; RaisePropertyChanged(); } }
        //public new MemberInfo SelectMember { get => selectMember; set { selectMember = value; RaisePropertyChanged(); } }


    }
}
